#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QKeyEvent>
#include <QFile>
#include <QDateTime>
#include <QTime>
#include <QDebug>
#include <QDir>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->textEdit->installEventFilter(this);
    showDays();
    QDateTime time = QDateTime::currentDateTime();
    showIdeas(time.toString("yyyy-MM-dd"));
}

MainWindow::~MainWindow()
{
    delete ui;
}
// if key == 2 mean ctrl + enter
int key = 0;
void MainWindow::keyPressEvent(QKeyEvent* event) {
    if(event->key() == Qt::Key_Control) {
        key++;
    }
}

void MainWindow::keyReleaseEvent(QKeyEvent* event) {
    if(event->key() == Qt::Key_Control) {
        key--;
    }
}

bool MainWindow::showDays() {
    ui->comboBox->clear();
    QDir dir;
    QString dir_str = "./data/";
    if(!dir.exists(dir_str)) {
        bool res = dir.mkpath(dir_str);
    }
    dir.setPath(dir_str);
    QStringList dirs = dir.entryList(QDir::Dirs, QDir::Name);
    for(auto dir: dirs) {
        if(dir.startsWith(".") || dirs.startsWith("..")) continue;
        ui->comboBox->addItem(dir);
        QDateTime time = QDateTime::currentDateTime();
        if(dir == time.toString("yyyy-MM-dd")) {
            ui->comboBox->setCurrentText(dir);
        }
    }

}

bool MainWindow::submitIdea() {
    QString idea = ui->textEdit->toPlainText();
    QFile f;
    QDateTime time = QDateTime::currentDateTime();
    QDir dir;
    QString dir_str = "./data/" + time.toString("yyyy-MM-dd");
    if(!dir.exists(dir_str)) {
        bool res = dir.mkpath(dir_str);
        if(res) {
            // new day
            showDays();
        }
    }



    QString filename = dir_str + "/" + time.toString("yyyy-MM-dd-hh-mm-ss") + ".txt";

    f.setFileName(filename);
    if(f.open(QIODevice::WriteOnly)) {
        QTextStream in(&f);
        in.setEncoding(QStringConverter::Utf8);
        in << idea;
        f.close();
    } else {
        qDebug() << f.errorString();
    }
    ui->textEdit->clear();

    QString dirname = time.toString("yyyy-MM-dd");
    showIdeas(dirname);
}

bool MainWindow::showIdeas(QString dirname) {
    // update the list by the name of dirname
    ui->listWidget->clear();
    QString idea;
    QDateTime time = QDateTime::currentDateTime();
    QDir dir;
    QString dir_str = "./data/" + dirname;
    if(!dir.exists(dir_str)) {
        bool res = dir.mkpath(dir_str);
    }
    QDir new_dir(dir_str);
    QStringList files = new_dir.entryList(QDir::Files|QDir::Readable, QDir::Name);

    for(auto file: files) {
        qDebug() << file;
    }

    QStringList ideaList;
    QFile f;
    for(auto file : files) {
        f.setFileName(dir_str + "/" + file);
        if(f.open(QIODevice::ReadOnly | QIODevice::WriteOnly)) {
            QTextStream out(&f);
            out.setEncoding(QStringConverter::Utf8);
            idea = out.readAll();
            qDebug() << idea;
            ideaList.push_back(file + "\n" + idea);
            f.close();
        } else {
            // open error
        }
    }
    for(auto idea : ideaList) {
        ui->listWidget->addItem(idea);
        ui->listWidget->scrollToBottom();
    }
}

bool MainWindow::eventFilter(QObject *target, QEvent *event)
{
    if(target == ui->textEdit) {
        if(event->type() == QEvent::KeyPress) {

            QKeyEvent *k = static_cast<QKeyEvent *>(event);

            if(k->key() == Qt::Key_Return && key == 1) {
                submitIdea();
                return true;
            }
        }
    }
    return QWidget::eventFilter(target, event);
}


void MainWindow::on_textEdit_textChanged()
{

}


void MainWindow::on_comboBox_activated(int index)
{
    // no need to update the comboBox self because when the first submit of the new days generate
    // comboBox will update and select the new day.
    showIdeas(ui->comboBox->currentText());
    qDebug() << ui->comboBox->currentText();
}

